package com.ice.app.system.dict.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;
import com.ice.app.system.constants.CommonConstant;
import com.ice.app.system.constants.SystemConstant;
import com.ice.app.system.dict.vo.DictVo;
import com.ice.app.system.entity.DictData;
import com.ice.app.system.mapper.DictDataMapper;
import com.ice.app.system.utils.I18NUtils;
import com.iceframework.core.utils.KeyUtils;
import com.iceframework.core.utils.WebUtils;

/**
 * 
 * 业务逻辑层： 数据字典子表
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@Service
public class DictDataService {

	@Autowired
	private DictDataMapper dictDataMapper;

	// ----------------------------------------自动生成--------------------------------------------

	/**
	 * 菜单保存
	 * 
	 * @param dictData
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public boolean saveDictData(DictData dictData) throws Exception {
		if (StringUtil.isNotEmpty(dictData.getId())) {
			// 修改操作
			DictData old = this.getDictData(dictData.getId());
			dictData.setUpdateBy(WebUtils.getUserName());
			dictData.setUpdateDate(new Date());
			if (old != null) {
				return dictDataMapper.updateByPrimaryKeySelective(dictData) > 0;
			}
			return false;
		} else {
			// 保存操作
			String key = KeyUtils.genKey("DictData");
			dictData.setId(key);
			dictData.setStatus(CommonConstant.SYS_STATUS_VALID);
			dictData.setCreateBy(WebUtils.getUserName());
			dictData.setCreateDate(new Date());
			dictData.setUpdateBy(WebUtils.getUserName());
			dictData.setUpdateDate(new Date());
			return dictDataMapper.insertSelective(dictData) > 0;

		}
	}

	/**
	 * 根据主键删除菜单
	 * 
	 * @param key
	 * @return
	 */
	@Transactional
	public boolean deleteDictData(String key) {
		return dictDataMapper.deleteByPrimaryKey(key) > 0;
	}

	/**
	 * 根据多个主键删除菜单
	 * 
	 * @param keys
	 * @return
	 */
	@Transactional
	public boolean deleteDictDatas(String keys) {
		if (!StringUtils.isEmpty(keys)) {
			String[] strs = keys.split(",");
			for (String key : strs) {
				this.deleteDictData(key);
			}
			return true;
		}
		return false;
	}

	/**
	 * 根据主键获取菜单实体
	 * 
	 * @param key
	 * @return
	 */
	public DictData getDictData(String key) {
		return dictDataMapper.selectByPrimaryKey(key);
	}

	/**
	 * 根据给定条件获取菜单实体集合
	 * 
	 * @param key
	 * @return
	 */
	public List<DictData> getDictDatas(DictData dictData) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dictData", dictData);
		return dictDataMapper.selectByFilter(map);
	}
	
	/**
	 * 根据给定条件获取菜单实体集合
	 * 
	 * @param key
	 * @return
	 */
	public List<DictData> getDictDatasByDictCode(String dictCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		DictData dictData = new DictData();
		dictData.setDictCode(dictCode);
		map.put("dictData", dictData);
		return dictDataMapper.selectByFilter(map);
	}

	// ----------------------------------------自定义（Public）---------------------------------------------


	/**
	 * 根据字典Code取字典项
	 * @param dictCode
	 * @return
	 */
	public List<DictVo> getDictVoByCode(String dictCode) {
		String i18nLanguage = I18NUtils.getI18NLanguage();
		List<DictData> dictDatasByDictCode = this.getDictDatasByDictCode(dictCode);
		List<DictVo> dictVoList = new ArrayList<DictVo>();
		for (DictData dictData : dictDatasByDictCode) {
			DictVo dictVo = new DictVo();
			dictVo.setValue(dictData.getValue());
			if(i18nLanguage.equals(SystemConstant.LANGUAGE_ZH)) {
				dictVo.setText(dictData.getName());
			} else {
				dictVo.setText(dictData.getI18n());
			}
			dictVo.setSort(String.valueOf(dictData.getOrderNum()));
			dictVo.setDisabled(dictData.getStatus());
			dictVoList.add(dictVo);
		}
		return dictVoList;
	}
}